-- 63.lua
-- Created by zouyb Jun/26/2015
-- 海怪触手BUFF光效

return {
    onAppear = function(monster)    
		-- 海怪触手出现时，幽灵船播放"特殊"光效

		-- 当前关卡
		local uiCurLevel = UIDungeonMgr.getCurLevel();
		
	    -- 当前怪物的基类ID
		local baseClassId = monster:query("base_class_id");

		-- 计算当前海怪触手的个数	
        local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
        local count = 0;
        for i = 1, #openMonsterGrids do
            local monsterGrid = openMonsterGrids[i];
            if monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
                count = count + 1;
            end
        end

        if count == 1 then
            if ME.user.ship ~= nil and not DungeonInstanceM.isInInstance() then
				local grid = UIDungeonMgr:getCurLevel().grids[ME.user.ship:getPos()];
				if grid ~= nil then
					-- 增加束缚光效（TODO 暂用章鱼替代）	
					local _, effect = playEffect(grid, 4022);
					grid.effect = effect;
				end
			end
    	end	
    end,

    onDie = function(monster)    
	    -- 当前怪物的基类ID
		local baseClassId = monster:query("base_class_id");

		-- 计算当前海怪触手的个数	
        local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
        local count = 0;
		local curMonsterGrid;
        for i = 1, #openMonsterGrids do
            local monsterGrid = openMonsterGrids[i];
            if monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
				curMonsterGrid = monsterGrid;
                count = count + 1;
            end
        end

		if count == 0 then
			-- 场上只剩下一只海怪触手，在该海怪触手死亡时，幽灵船播放"特殊"光效
            if ME.user.ship ~= nil and not DungeonInstanceM.isInInstance() then
				local grid = UIDungeonMgr:getCurLevel().grids[ME.user.ship:getPos()];
				if grid ~= nil then
				    -- 去除束缚光效
					if grid.effect ~= nil then
					   grid.effect:removeFromParent();
					   grid.effect = nil;
					end
				end
			end
		end
    end,
};